#
#    Copyright (C) 2016 Francisco Javier Parra <franparpe@openmailbox.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#

import math
import numpy
from qtcam.core.UnitChanger import Radiants2Degrees
from qtcam.core.parameters import SectionParameters


class HarmonicSection(SectionParameters):
    """This object represents the harmonic section of a cam
    """
    def __init__(self, LowestPosition=0.0,
                 HighestPosition=50.0, StartAngle=0.0,
                 EndAngle=100.0, Precision=0.1, AngularVelocity=1):
        "Initializes all necessary paramaters for further calculation"
        super(HarmonicSection, self).__init__(LowestPosition,
                                              HighestPosition,
                                              StartAngle,
                                              EndAngle,
                                              Precision,
                                              AngularVelocity)

    def Position(self, currentAngle):
        return (self.LowestPosition +
                (self.HighestPosition/2) *
                (1 - math.cos(math.pi *
                              currentAngle /
                              self.EndAngle)))

    def ArrayPosition(self,):  # Look Here
        return (self.LowestPosition +
                (self.HighestPosition/2) *
                (1 - numpy.cos(numpy.pi *
                               (self.abcissaeValues - self.StartAngle) /
                               (self.EndAngle - self.StartAngle))))

    def Velocity(self, currentAngle):
        angularVelocityDeg = Radiants2Degrees(self.AngularVelocity)
        return (angularVelocityDeg *
                (math.pi *
                 self.HighestPosition / (2 * self.EndAngle)) *
                math.sin((math.pi * currentAngle) / self.EndAngle))

    def ArrayVelocity(self):
        theta = self.abcissaeValues - self.StartAngle
        beta = self.EndAngle - self.StartAngle
        return (self.AngularVelocity *
                (numpy.pi *
                 self.HighestPosition / (2 * math.radians(beta))) *
                numpy.sin((math.pi * theta) /
                          beta))

    def Acceleration(self, currentAngle):
        angularVelocityDeg = Radiants2Degrees(self.AngularVelocity)
        return (math.power(angularVelocityDeg, 2) *
                (math.power(math.pi, 2) *
                 self.HighestPosition / (2 * math.power(self.EndAngle, 2))) *
                math.cos((math.pi * currentAngle) / self.EndAngle))

    def ArrayAcceleration(self):
        theta = self.abcissaeValues - self.StartAngle
        beta = self.EndAngle - self.StartAngle
        return (numpy.power(self.AngularVelocity, 2) *
                (numpy.power(math.pi, 2) *
                 self.HighestPosition / (2 *
                                         numpy.power(math.radians(beta), 2))) *
                numpy.cos((math.pi * theta) /
                          beta))

    def OverAcceleration(self, currentAngle):
        angularVelocityDeg = Radiants2Degrees(self.AngularVelocity)
        return -1 * (math.power(angularVelocityDeg, 3) *
                     (math.power(math.pi, 3) *
                      self.HighestPosition / (2 *
                                              math.power(self.EndAngle, 3))) *
                     math.sin((math.pi * currentAngle) / self.EndAngle))

    def ArrayOverAcceleration(self):
        theta = self.abcissaeValues - self.StartAngle
        beta = self.EndAngle - self.StartAngle
        return -1 * (numpy.power(self.AngularVelocity, 3) *
                     (numpy.power(math.pi, 3) *
                      self.HighestPosition / (2 *
                                              numpy.power(math.radians(beta),
                                                          3))) *
                     numpy.sin((numpy.pi * theta) / beta))
